package com.itheima.controller;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class CookieController {
    @RequestMapping("/setCookie")
    public void setCookie(HttpServletResponse response){
        response.addCookie(new Cookie("username","joy"));
    }

    @GetMapping("/getCookie")
    public void getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if(cookie.getName().equals("username")){
                log.info("username:{}",cookie.getValue());
                break;
            }
        }
    }
}
